﻿using System;
using System.Collections.Generic;
using System.Text;
using Bpms.Config;

namespace Bpms.Mail
{
    public class Email
    {
        protected static ISmtpMail ESM;
        protected static EmailConfigInfo emailinfo = ConfigContext.GetEmailConfigInfo();

        static Email()
        {
            try
            {
                // if (emailinfo.DllFileName.ToLower().IndexOf(".dll") <= 0)
                //{
                //  emailinfo.DllFileName = emailinfo.DllFileName + ".dll";
                //}
                ESM = (ISmtpMail)Activator.CreateInstance(Type.GetType(string.Format("{0},{1}", emailinfo.NameSpace, emailinfo.DllFileName), true, true));
            }
            catch
            {
                ESM = new SmtpMail();
            }
        }

        public static bool BpmsSmtpMail(string from, string fromName, string userName, string email, string title, string body)
        {
            try
            {
                ESM.RecipientName = userName;
                ESM.AddRecipient(email);
                ESM.MailDomainPort = emailinfo.Port;
                ESM.From = from;//设定发件人地址(必须填写)
                ESM.FromName = fromName;
                ESM.Html = true;//设定正文是否HTML格式。
                ESM.Subject = title;

                ESM.Body = "<pre style=\"width:100%;word-wrap:break-word\">" + body.ToString() + "</pre>";

                ESM.MailDomain = emailinfo.Smtp;
                ESM.MailServerUserName = emailinfo.UserName;
                ESM.MailServerPassWord = emailinfo.Password;

                //开始发送
                return ESM.Send();
            }
            catch
            {
                return false;
            }
        }

        public static bool BpmsSystemSmtpMail(string userName, string email, string title, string body)
        {
            try
            {
                ESM.RecipientName = userName;
                ESM.AddRecipient(email);
                ESM.MailDomainPort = emailinfo.Port;
                ESM.From = emailinfo.SysEmail;//设定发件人地址(必须填写)
                ESM.FromName = emailinfo.SyseName;
                ESM.Html = true;//设定正文是否HTML格式。
                ESM.Subject = title;

                ESM.Body = "<pre style=\"width:100%;word-wrap:break-word\">" + body.ToString() + "</pre>";

                ESM.MailDomain = emailinfo.Smtp;
                ESM.MailServerUserName = emailinfo.UserName;
                ESM.MailServerPassWord = emailinfo.Password;

                //开始发送
                return ESM.Send();
            }
            catch
            {
                return false;
            }
        }

    }
}
